package com.lihao.wanandroid.ui.project

import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.kingja.loadsir.core.LoadService
import com.lihao.jetpackcore.ext.getViewModel
import com.lihao.jetpackcore.ext.nav
import com.lihao.jetpackcore.ext.parseState
import com.lihao.jetpackcore.ext.util.dp2px
import com.lihao.wanandroid.R
import com.lihao.wanandroid.app.base.BaseFragment
import com.lihao.wanandroid.app.ext.*
import com.lihao.wanandroid.app.weight.recyclerview.SpaceItemDecoration
import com.lihao.wanandroid.ui.collect.CollectViewModel
import com.lihao.wanandroid.ui.web.WebFragment
import kotlinx.android.synthetic.main.fragment_project_new.*

/**
 * 项目列表: 项目分类下的项目列表和新项目列表
 */
class ProjectListFragment : BaseFragment<ProjectViewModel>() {

    private lateinit var loadSir: LoadService<Any>

    private val mAdapter: ProjectAdapter by lazy {
        ProjectAdapter().apply {
            setAdapterAnimation(2)
            loadMoreModule.setOnLoadMoreListener {
                mViewModel.autoGetProjectList(false)
            }
            setOnItemClickListener { adapter, view, position ->
                nav().navigate(R.id.action_to_webFragment, Bundle().apply {
                    putParcelable(WebFragment.KEY_ARTICLE, mAdapter.data[position])
                })
            }
        }
    }

    override fun layoutId() = R.layout.fragment_project_new

    override fun initView(savedInstanceState: Bundle?) {
        arguments?.run {
            mViewModel.isNewProject = getBoolean("isNewProject")
            mViewModel.id = getInt("id")
        }
        loadSir = LoadServiceInit(recyclerView) {
            loadSir.showLoading()
            mViewModel.autoGetProjectList(true)
        }
        swipeRefresh.setOnRefreshListener {
            mViewModel.autoGetProjectList(true)
        }
        recyclerView.init(LinearLayoutManager(context), mAdapter).run {
            addItemDecoration(SpaceItemDecoration(dp2px(8), dp2px(8), true))
        }
    }

    override fun lazyLoadData() {
        loadSir.showLoading()
        mViewModel.autoGetProjectList(true)
    }

    override fun createObserver() {
        mViewModel.projectList.observe(this, Observer { resultState ->
            parseState(resultState, {
                mViewModel.page++
                when {
                    it.isFirstEmpty() -> {
                        swipeRefresh.isRefreshing = false
                        loadSir.showEmpty()
                    }
                    it.isRefresh() -> {
                        swipeRefresh.isRefreshing = false
                        loadSir.showSuccess()
                        mAdapter.setList(it.datas)
                    }
                    else -> {
                        mAdapter.addData(it.datas)
                        if (it.hasMore()) {
                            mAdapter.loadMoreModule.loadMoreComplete()
                        } else {
                            mAdapter.loadMoreModule.loadMoreEnd()
                        }
                    }
                }
            }, {
                if (mViewModel.page.isRefresh()) {
                    swipeRefresh.isRefreshing = false
                    loadSir.showError(it.errorMsg)
                } else {
                    mAdapter.loadMoreModule.loadMoreFail()
                }
            })
        })

        shareViewModel.run {
            userInfo.observe(viewLifecycleOwner, Observer {
                if (it != null) {
                    it.collectIds.forEach { id ->
                        for (item in mAdapter.data) {
                            if (id.toInt() == item.id) {
                                item.collect = true
                                break
                            }
                        }
                    }
                } else {
                    for (item in mAdapter.data) {
                        item.collect = false
                    }
                    mAdapter.notifyDataSetChanged()
                }
            })
        }
    }

    companion object {

        fun newInstance(isNewProject: Boolean, id: Int = 0): ProjectListFragment {
            val fragment = ProjectListFragment()
            fragment.arguments = Bundle().apply {
                putBoolean("isNewProject", isNewProject)
                putInt("id", id)
            }
            return fragment
        }
    }
}